package com.dynamixsoftware.printershare;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.database.DataSetObserver;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.dynamixsoftware.printershare.data.DuplexMode;
import com.dynamixsoftware.printershare.data.Paper;
import com.dynamixsoftware.printershare.data.Printer;
import com.dynamixsoftware.printershare.data.PrintoutMode;
import com.dynamixsoftware.printershare.data.Tray;
import com.dynamixsoftware.printershare.data.User;
import com.google.android.googleapps.IGoogleLoginService;
import com.google.android.googleapps.IGoogleLoginService.Stub;
import com.google.android.googlelogin.GoogleLoginServiceConstants;
import java.io.BufferedInputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;

public class ActivityCloudPrinters
  extends ActivityCore
{
  private static int requesting_credentials;
  private String account;
  private String[] al;
  private Object[] al_objs;
  private String[] credentials;
  private boolean ftoken;
  private Handler handler;
  private Vector<Printer> printers;
  private Thread wt;
  
  private void authorize(String paramString)
  {
    try
    {
      Thread.sleep(250L);
      this.credentials = new String[2];
      this.credentials[0] = paramString;
      if (App.has_feature_gls)
      {
        Object localObject1 = getSystemService("account");
        if (localObject1 != null)
        {
          try
          {
            Object[] arrayOfObject1 = this.al_objs;
            int k = 0;
            if (arrayOfObject1 != null)
            {
              i1 = 0;
              int i2 = this.al.length;
              k = 0;
              if (i1 < i2)
              {
                if (!this.al[i1].equals(paramString)) {
                  break label916;
                }
                k = i1;
              }
            }
            Method[] arrayOfMethod = localObject1.getClass().getMethods();
            m = 0;
            int n = arrayOfMethod.length;
            Method localMethod = null;
            if (m < n)
            {
              if ((!"getAuthToken".equals(arrayOfMethod[m].getName())) || (arrayOfMethod[m].getParameterTypes().length != 5)) {
                break label922;
              }
              localMethod = arrayOfMethod[m];
            }
            Object[] arrayOfObject2 = new Object[5];
            arrayOfObject2[0] = this.al_objs[k];
            arrayOfObject2[1] = "cloudprint";
            arrayOfObject2[2] = Boolean.valueOf(false);
            arrayOfObject2[3] = null;
            arrayOfObject2[4] = this.handler;
            Object localObject2 = localMethod.invoke(localObject1, arrayOfObject2);
            Bundle localBundle = (Bundle)localObject2.getClass().getMethod("getResult", new Class[0]).invoke(localObject2, new Object[0]);
            Intent localIntent = (Intent)localBundle.getParcelable("intent");
            if (localIntent != null)
            {
              if (requesting_credentials == 0)
              {
                requesting_credentials = 1;
                startActivity(localIntent);
                this.wt = null;
              }
            }
            else {
              this.credentials[1] = localBundle.getString("authtoken");
            }
          }
          catch (Exception localException4)
          {
            for (;;)
            {
              localException4.printStackTrace();
              App.reportThrowable(localException4);
            }
          }
          requesting_credentials = 0;
          authorize_done();
          return;
        }
        try
        {
          int j = this.credentials[0].indexOf("");
          if (j > 0)
          {
            String str6 = this.credentials[0].substring(j + 1);
            this.credentials[0] = this.credentials[0].substring(0, j);
            String str7 = App.glsGetAuthToken("cloudprint", this.credentials[0], str6);
            if (str7 != null)
            {
              SharedPreferences.Editor localEditor2 = this.prefs.edit();
              localEditor2.putString("cloud_token_" + this.credentials[0], str7);
              localEditor2.commit();
            }
          }
          this.credentials[1] = this.prefs.getString("cloud_token_" + this.credentials[0], null);
          if (this.credentials[1] == null)
          {
            Runnable local8 = new Runnable()
            {
              public void run()
              {
                ActivityCloudPrinters.this.hideProgress();
                final View localView = LayoutInflater.from(ActivityCloudPrinters.this).inflate(2130903045, null);
                localView.findViewById(2131230742).setVisibility(0);
                EditText localEditText = (EditText)localView.findViewById(2131230739);
                localEditText.setText(ActivityCloudPrinters.this.credentials[0]);
                localEditText.setInputType(0);
                localEditText.setFocusable(false);
                new AlertDialog.Builder(ActivityCloudPrinters.this).setIcon(17301543).setTitle(2131099841).setView(localView).setCancelable(false).setPositiveButton(2131099703, new DialogInterface.OnClickListener()
                {
                  public void onClick(DialogInterface paramAnonymous2DialogInterface, int paramAnonymous2Int)
                  {
                    final String str = ((EditText)localView.findViewById(2131230741)).getText().toString();
                    ActivityCloudPrinters.this.showProgress(ActivityCloudPrinters.this.getResources().getString(2131099746));
                    ActivityCloudPrinters.access$202(ActivityCloudPrinters.this, new Thread()
                    {
                      public void run()
                      {
                        ActivityCloudPrinters.this.authorize(ActivityCloudPrinters.this.credentials[0] + "" + str);
                      }
                    });
                    ActivityCloudPrinters.this.wt.start();
                  }
                }).setNegativeButton(2131099707, new DialogInterface.OnClickListener()
                {
                  public void onClick(DialogInterface paramAnonymous2DialogInterface, int paramAnonymous2Int)
                  {
                    ActivityCloudPrinters.this.authorize_done();
                  }
                }).create().show();
              }
            };
            runOnUiThread(local8);
            return;
          }
        }
        catch (Exception localException3)
        {
          localException3.printStackTrace();
          App.reportThrowable(localException3);
          authorize_done();
          return;
        }
      }
      try
      {
        if (this.credentials[0] != null)
        {
          int i = this.credentials[0].indexOf("");
          if (i > 0)
          {
            str1 = this.credentials[0].substring(i + 1);
            this.credentials[0] = this.credentials[0].substring(0, i);
            String str2 = App.glsGetAuthToken("cloudprint", this.credentials[0], str1);
            if (str2 != null)
            {
              localEditor1 = this.prefs.edit();
              localEditor1.putString("cloud_token_" + this.credentials[0], str2);
            }
          }
        }
      }
      catch (Exception localException1)
      {
        String str1;
        SharedPreferences.Editor localEditor1;
        String str3;
        String str4;
        String str5;
        label809:
        Runnable local9;
        localException1.printStackTrace();
        App.reportThrowable(localException1);
        authorize_done();
        return;
      }
      try
      {
        str3 = App.glsGetAuthToken("mail", this.credentials[0], str1);
        if (str3 != null) {
          localEditor1.putString("gmail_token_" + this.credentials[0], str3);
        }
        str4 = App.glsGetAuthToken("writely", this.credentials[0], str1);
        if (str4 != null) {
          localEditor1.putString("gdocs_token1_" + this.credentials[0], str4);
        }
        str5 = App.glsGetAuthToken("wise", this.credentials[0], str1);
        if (str5 != null) {
          localEditor1.putString("gdocs_token2_" + this.credentials[0], str5);
        }
      }
      catch (Exception localException2)
      {
        localException2.printStackTrace();
        App.reportThrowable(localException2);
        break label809;
      }
      localEditor1.commit();
      this.credentials[1] = this.prefs.getString("cloud_token_" + this.credentials[0], null);
      if (this.credentials[1] == null)
      {
        local9 = new Runnable()
        {
          public void run()
          {
            ActivityCloudPrinters.this.hideProgress();
            final View localView = LayoutInflater.from(ActivityCloudPrinters.this).inflate(2130903045, null);
            localView.findViewById(2131230742).setVisibility(0);
            EditText localEditText = (EditText)localView.findViewById(2131230739);
            localEditText.setText(ActivityCloudPrinters.this.credentials[0]);
            if (ActivityCloudPrinters.this.prefs.getString("cloud_account", null) != null)
            {
              localEditText.setInputType(0);
              localEditText.setFocusable(false);
            }
            new AlertDialog.Builder(ActivityCloudPrinters.this).setIcon(17301543).setTitle(2131099841).setView(localView).setCancelable(false).setPositiveButton(2131099703, new DialogInterface.OnClickListener()
            {
              public void onClick(DialogInterface paramAnonymous2DialogInterface, int paramAnonymous2Int)
              {
                final String str1 = ((EditText)localView.findViewById(2131230739)).getText().toString();
                final String str2 = ((EditText)localView.findViewById(2131230741)).getText().toString();
                ActivityCloudPrinters.this.showProgress(ActivityCloudPrinters.this.getResources().getString(2131099746));
                ActivityCloudPrinters.access$202(ActivityCloudPrinters.this, new Thread()
                {
                  public void run()
                  {
                    ActivityCloudPrinters.this.authorize(str1 + "" + str2);
                  }
                });
                ActivityCloudPrinters.this.wt.start();
              }
            }).setNegativeButton(2131099707, new DialogInterface.OnClickListener()
            {
              public void onClick(DialogInterface paramAnonymous2DialogInterface, int paramAnonymous2Int)
              {
                ActivityCloudPrinters.this.authorize_done();
              }
            }).create().show();
          }
        };
        runOnUiThread(local9);
        return;
      }
    }
    catch (InterruptedException localInterruptedException)
    {
      for (;;)
      {
        int i1;
        int m;
        continue;
        label916:
        i1++;
        continue;
        label922:
        m++;
      }
    }
  }
  
  private void authorize_done()
  {
    this.last_error = null;
    int n;
    if (this.credentials[1] != null) {
      if (this.prefs.getString("cloud_account", null) == null)
      {
        n = 1;
        String str2 = this.prefs.getString("gls_account_" + n, null);
        if ((str2 != null) && (!str2.equals(this.credentials[0]))) {
          break label978;
        }
        SharedPreferences.Editor localEditor4 = this.prefs.edit();
        localEditor4.putString("gls_account_" + n, this.credentials[0]);
        localEditor4.putString("cloud_account", this.credentials[0]);
        localEditor4.commit();
      }
    }
    for (;;)
    {
      StatusLine localStatusLine;
      boolean bool;
      try
      {
        BasicHttpParams localBasicHttpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(localBasicHttpParams, 15000);
        HttpConnectionParams.setSoTimeout(localBasicHttpParams, 15000);
        DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient(localBasicHttpParams);
        HttpGet localHttpGet = new HttpGet("https://www.google.com/cloudprint/search?output=json");
        localHttpGet.setHeader("User-Agent", App.getUserAgent());
        localHttpGet.setHeader("Authorization", "GoogleLogin auth=" + this.credentials[1]);
        localHttpGet.setHeader("X-CloudPrint-Proxy", "PrinterShare");
        HttpResponse localHttpResponse = localDefaultHttpClient.execute(localHttpGet);
        localStatusLine = localHttpResponse.getStatusLine();
        i = localStatusLine.getStatusCode();
        if (i != 200) {
          continue;
        }
        GPrinterList localGPrinterList = (GPrinterList)Json.read(new BufferedInputStream(localHttpResponse.getEntity().getContent()), GPrinterList.class);
        if (localGPrinterList.printers != null)
        {
          int j = 0;
          if (j < localGPrinterList.printers.size())
          {
            GPrinter localGPrinter = (GPrinter)localGPrinterList.printers.get(j);
            Printer localPrinter = new Printer();
            localPrinter.id = (localGPrinter.id + "@cloudprint.google.");
            if ((localGPrinter.status == null) || ("".equals(localGPrinter.status))) {
              break label972;
            }
            if (!"0".equals(localGPrinter.status)) {
              break label984;
            }
            break label972;
            localPrinter.online = bool;
            if (localGPrinter.displayName != null)
            {
              str1 = localGPrinter.displayName;
              localPrinter.title = fixEncoding(str1);
              localPrinter.model = fixEncoding(localGPrinter.name);
              localPrinter.owner = new User();
              localPrinter.owner.name = "Google Cloud";
              this.printers.add(localPrinter);
              j++;
              continue;
            }
            String str1 = localGPrinter.name;
            continue;
          }
          SharedPreferences.Editor localEditor1 = this.prefs.edit();
          localEditor1.putString("cloud_token", this.credentials[1]);
          localEditor1.commit();
        }
      }
      catch (Exception localException1)
      {
        int i;
        Runnable local11;
        Object localObject;
        int k;
        localException1.printStackTrace();
        App.reportThrowable(localException1);
        continue;
        k++;
        continue;
        SharedPreferences.Editor localEditor3 = this.prefs.edit();
        localEditor3.remove("cloud_token_" + this.credentials[0]);
        localEditor3.commit();
        authorize(this.credentials[0]);
        return;
      }
      local11 = new Runnable()
      {
        public void run()
        {
          TextView localTextView = (TextView)ActivityCloudPrinters.this.findViewById(2131230804);
          String str = ActivityCloudPrinters.this.getResources().getString(2131099753);
          Object[] arrayOfObject = new Object[1];
          arrayOfObject[0] = Integer.valueOf(ActivityCloudPrinters.this.printers.size());
          localTextView.setText(String.format(str, arrayOfObject));
          ((ActivityCloudPrinters.PrinterAdapter)((ListView)ActivityCloudPrinters.this.findViewById(2131230805)).getAdapter()).fireOnChanged();
          ActivityCloudPrinters.this.hideProgress();
          if (!ActivityCloudPrinters.this.isFinishing())
          {
            if (ActivityCloudPrinters.this.last_error == null) {
              break label120;
            }
            ActivityCloudPrinters.this.displayLastError(new DialogInterface.OnClickListener()
            {
              public void onClick(DialogInterface paramAnonymous2DialogInterface, int paramAnonymous2Int)
              {
                ActivityCloudPrinters.this.findViewById(2131230803).showContextMenu();
              }
            });
          }
          label120:
          while (ActivityCloudPrinters.this.printers.size() != 0) {
            return;
          }
          new AlertDialog.Builder(ActivityCloudPrinters.this).setIcon(17301659).setTitle(2131099846).setMessage(2131099850).setPositiveButton(2131099703, null).setNeutralButton(2131099710, new DialogInterface.OnClickListener()
          {
            public void onClick(DialogInterface paramAnonymous2DialogInterface, int paramAnonymous2Int)
            {
              Intent localIntent = new Intent();
              localIntent.setAction("android.intent.action.VIEW");
              localIntent.addCategory("android.intent.category.BROWSABLE");
              localIntent.setData(Uri.parse("https://www.google.com/support/cloudprint/"));
              ActivityCloudPrinters.this.startActivity(localIntent);
            }
          }).show();
        }
      };
      runOnUiThread(local11);
      this.wt = null;
      return;
      if (((i == 403) || (i == 401) || ("Token expired".equalsIgnoreCase(localStatusLine.getReasonPhrase()))) && (!this.ftoken))
      {
        this.ftoken = true;
        if (App.has_feature_gls)
        {
          localObject = getSystemService("account");
          if (localObject != null)
          {
            try
            {
              Method[] arrayOfMethod = localObject.getClass().getMethods();
              k = 0;
              int m = arrayOfMethod.length;
              Method localMethod = null;
              if (k < m)
              {
                if ((!"invalidateAuthToken".equals(arrayOfMethod[k].getName())) || (arrayOfMethod[k].getParameterTypes().length != 2)) {
                  continue;
                }
                localMethod = arrayOfMethod[k];
              }
              Object[] arrayOfObject = new Object[2];
              arrayOfObject[0] = "com.google";
              arrayOfObject[1] = this.credentials[1];
              localMethod.invoke(localObject, arrayOfObject);
              authorize(this.credentials[0]);
              return;
            }
            catch (Exception localException2)
            {
              localException2.printStackTrace();
              App.reportThrowable(localException2);
            }
            continue;
          }
        }
        SharedPreferences.Editor localEditor2 = this.prefs.edit();
        localEditor2.remove("cloud_token_" + this.credentials[0]);
        localEditor2.commit();
        authorize(this.credentials[0]);
      }
      else
      {
        this.last_error = ("Error: " + localStatusLine.getStatusCode() + " " + localStatusLine.getReasonPhrase());
        continue;
        this.last_error = "Error: Authorization failed or network error has occured";
        continue;
        label972:
        bool = true;
        continue;
        label978:
        n++;
        break;
        label984:
        bool = false;
      }
    }
  }
  
  public static String fixEncoding(String paramString)
  {
    if (paramString != null) {}
    try
    {
      char[] arrayOfChar = new char[paramString.length()];
      paramString.getChars(0, paramString.length(), arrayOfChar, 0);
      byte[] arrayOfByte = new byte[arrayOfChar.length];
      for (int i = 0; i < arrayOfByte.length; i++) {
        arrayOfByte[i] = ((byte)(0xFF & arrayOfChar[i]));
      }
      String str = new String(arrayOfByte, "UTF-8");
      paramString = str;
      return paramString;
    }
    catch (UnsupportedEncodingException localUnsupportedEncodingException) {}
    return paramString;
  }
  
  public static Printer loadPrinter(GPrinter paramGPrinter)
  {
    Printer localPrinter = new Printer();
    localPrinter.id = (paramGPrinter.id + "@cloudprint.google.");
    boolean bool;
    if ((paramGPrinter.status == null) || ("".equals(paramGPrinter.status)) || ("0".equals(paramGPrinter.status))) {
      bool = true;
    }
    for (;;)
    {
      localPrinter.online = bool;
      String str1;
      label87:
      int i;
      label137:
      GCapability localGCapability;
      int n;
      label199:
      GOption localGOption4;
      Paper localPaper;
      String str2;
      String str3;
      if (paramGPrinter.displayName != null)
      {
        str1 = paramGPrinter.displayName;
        localPrinter.title = fixEncoding(str1);
        localPrinter.model = fixEncoding(paramGPrinter.name);
        localPrinter.owner = new User();
        localPrinter.owner.name = "Google Cloud";
        if (paramGPrinter.capabilities == null) {
          break label1254;
        }
        i = 0;
        if (i >= paramGPrinter.capabilities.size()) {
          break label1254;
        }
        localGCapability = (GCapability)paramGPrinter.capabilities.get(i);
        if (!"psk:PageMediaSize".equals(localGCapability.name)) {
          break label547;
        }
        localPrinter.papers = new Vector();
        if (localGCapability.options == null) {
          break label547;
        }
        n = 0;
        if (n >= localGCapability.options.size()) {
          break label540;
        }
        localGOption4 = (GOption)localGCapability.options.get(n);
        localPaper = new Paper();
        localPaper.id = localGOption4.name;
        if (localGOption4.$other != null) {
          localPaper.name = fixEncoding((String)localGOption4.$other.get("psk:DisplayName"));
        }
        localPaper.drv_params = ("{\"type\":\"" + localGCapability.type + "\",\"name\":\"" + localGCapability.name + "\",\"options\":[{\"name\":\"" + localGOption4.name + "\"}]}");
        if ((localGOption4.scoredProperties != null) && (localGOption4.scoredProperties.$other != null))
        {
          str2 = (String)localGOption4.scoredProperties.$other.get("psk:MediaSizeWidth");
          str3 = (String)localGOption4.scoredProperties.$other.get("psk:MediaSizeHeight");
          if (str2 == null) {}
        }
      }
      try
      {
        localPaper.width = (Integer.parseInt(str2) / 100);
        label414:
        if (str3 != null) {}
        try
        {
          localPaper.height = (Integer.parseInt(str3) / 100);
          label432:
          localPaper.image_area = new Rect(0, 0, localPaper.width, localPaper.height);
          if ((localPaper.id != null) && (localPaper.name != null) && (localPaper.width > 0) && (localPaper.height > 0))
          {
            localPrinter.papers.add(localPaper);
            if ("true".equals(localGOption4.default_)) {
              localPrinter.default_paper = localPaper.id;
            }
          }
          n++;
          break label199;
          bool = false;
          continue;
          str1 = paramGPrinter.name;
          break label87;
          label540:
          Collections.sort(localPrinter.papers);
          label547:
          if ("psk:JobInputBin".equals(localGCapability.name))
          {
            localPrinter.trays = new Vector();
            if (localGCapability.options != null) {
              for (int m = 0; m < localGCapability.options.size(); m++)
              {
                GOption localGOption3 = (GOption)localGCapability.options.get(m);
                Tray localTray2 = new Tray();
                localTray2.id = localGOption3.name;
                if (localGOption3.$other != null) {
                  localTray2.name = fixEncoding((String)localGOption3.$other.get("psk:DisplayName"));
                }
                localTray2.drv_params = ("{\"type\":\"" + localGCapability.type + "\",\"name\":\"" + localGCapability.name + "\",\"options\":[{\"name\":\"" + localGOption3.name + "\"}]}");
                if ((localTray2.id != null) && (localTray2.name != null))
                {
                  localPrinter.trays.add(localTray2);
                  if ("true".equals(localGOption3.default_)) {
                    localPrinter.default_tray = localTray2.id;
                  }
                }
              }
            }
          }
          if ("psk:PageOutputColor".equals(localGCapability.name))
          {
            localPrinter.printoutModes = new Vector();
            if (localGCapability.options != null) {
              for (int k = 0; k < localGCapability.options.size(); k++)
              {
                GOption localGOption2 = (GOption)localGCapability.options.get(k);
                PrintoutMode localPrintoutMode2 = new PrintoutMode();
                localPrintoutMode2.id = localGOption2.name;
                if (localGOption2.$other != null) {
                  localPrintoutMode2.name = fixEncoding((String)localGOption2.$other.get("psk:DisplayName"));
                }
                localPrintoutMode2.drv_params = ("{\"type\":\"" + localGCapability.type + "\",\"name\":\"" + localGCapability.name + "\",\"options\":[{\"name\":\"" + localGOption2.name + "\"}]}");
                localPrintoutMode2.resolution = "150x150";
                if ((localPrintoutMode2.id != null) && (localPrintoutMode2.name != null))
                {
                  localPrinter.printoutModes.add(localPrintoutMode2);
                  if ("true".equals(localGOption2.default_)) {
                    localPrinter.default_printoutMode = localPrintoutMode2.id;
                  }
                }
              }
            }
          }
          if ("psk:JobDuplexAllDocumentsContiguously".equals(localGCapability.name))
          {
            localPrinter.duplexModes = new Vector();
            if (localGCapability.options != null) {
              for (int j = 0; j < localGCapability.options.size(); j++)
              {
                GOption localGOption1 = (GOption)localGCapability.options.get(j);
                DuplexMode localDuplexMode2 = new DuplexMode();
                localDuplexMode2.id = localGOption1.name;
                if (localGOption1.$other != null) {
                  localDuplexMode2.name = fixEncoding((String)localGOption1.$other.get("psk:DisplayName"));
                }
                localDuplexMode2.drv_params = ("{\"type\":\"" + localGCapability.type + "\",\"name\":\"" + localGCapability.name + "\",\"options\":[{\"name\":\"" + localGOption1.name + "\"}]}");
                if ((localDuplexMode2.id != null) && (localDuplexMode2.name != null))
                {
                  localPrinter.duplexModes.add(localDuplexMode2);
                  if ("true".equals(localGOption1.default_)) {
                    localPrinter.default_duplexMode = localDuplexMode2.id;
                  }
                }
              }
            }
          }
          i++;
          break label137;
          label1254:
          if ((localPrinter.papers == null) || (localPrinter.papers.size() == 0))
          {
            localPrinter.papers = new Vector();
            localPrinter.default_paper = "Letter";
            localPrinter.papers.add(new Paper("Photo", "Photo, 4x6 inch", 1016, 1524, new Rect(0, 0, 1016, 1524)));
            localPrinter.papers.add(new Paper("L", "L, 3.5x5 inch", 889, 1270, new Rect(0, 0, 889, 1270)));
            localPrinter.papers.add(new Paper("Letter", "Letter", 2159, 2794, new Rect(0, 0, 2159, 2794)));
            localPrinter.papers.add(new Paper("A4", "A4", 2099, 2970, new Rect(0, 0, 2099, 2970)));
            localPrinter.papers.add(new Paper("Legal", "Legal", 2159, 3556, new Rect(0, 0, 2159, 3556)));
            localPrinter.papers.add(new Paper("A3", "A3", 2970, 4198, new Rect(0, 0, 2970, 4198)));
            localPrinter.papers.add(new Paper("Ledger", "Ledger", 2794, 4318, new Rect(0, 0, 2794, 4318)));
            localPrinter.papers.add(new Paper("B4", "B4", 2571, 3644, new Rect(0, 0, 2571, 3644)));
            Collections.sort(localPrinter.papers);
          }
          if ((localPrinter.trays == null) || (localPrinter.trays.size() == 0))
          {
            localPrinter.trays = new Vector();
            localPrinter.default_tray = "Default";
            Tray localTray1 = new Tray();
            localTray1.id = "Default";
            localTray1.name = "Default Tray";
            localPrinter.trays.add(localTray1);
          }
          if ((localPrinter.printoutModes == null) || (localPrinter.printoutModes.size() == 0))
          {
            localPrinter.printoutModes = new Vector();
            localPrinter.default_printoutMode = "Default";
            PrintoutMode localPrintoutMode1 = new PrintoutMode();
            localPrintoutMode1.id = "Default";
            localPrintoutMode1.name = "Default";
            localPrintoutMode1.resolution = "150x150";
            localPrinter.printoutModes.add(localPrintoutMode1);
          }
          if ((localPrinter.duplexModes == null) || (localPrinter.duplexModes.size() == 0))
          {
            localPrinter.duplexModes = new Vector();
            localPrinter.default_duplexMode = "None";
            DuplexMode localDuplexMode1 = new DuplexMode();
            localDuplexMode1.id = "None";
            localDuplexMode1.name = "Off";
            localPrinter.duplexModes.add(localDuplexMode1);
          }
          return localPrinter;
        }
        catch (NumberFormatException localNumberFormatException1)
        {
          break label432;
        }
      }
      catch (NumberFormatException localNumberFormatException2)
      {
        break label414;
      }
    }
  }
  
  private void load_printers(String paramString)
  {
    showProgress(getResources().getString(2131099746));
    this.account = paramString;
    SharedPreferences.Editor localEditor = this.prefs.edit();
    localEditor.putString("cloud_account", paramString);
    localEditor.commit();
    this.printers.clear();
    ((TextView)findViewById(2131230804)).setText("");
    ((PrinterAdapter)((ListView)findViewById(2131230805)).getAdapter()).fireOnChanged();
    this.wt = new Thread()
    {
      public void run()
      {
        ActivityCloudPrinters.this.authorize(ActivityCloudPrinters.this.account);
      }
    };
    this.wt.start();
  }
  
  private void xinit()
  {
    try
    {
      Thread.sleep(500L);
      label6:
      if (App.has_feature_gls)
      {
        Object localObject = getSystemService("account");
        if (localObject != null) {
          try
          {
            Object[] arrayOfObject = (Object[])localObject.getClass().getMethod("getAccountsByType", new Class[] { String.class }).invoke(localObject, new Object[] { "com.google" });
            this.al_objs = arrayOfObject;
            if (arrayOfObject != null)
            {
              this.al = new String[arrayOfObject.length];
              if (arrayOfObject.length > 0)
              {
                Field localField = arrayOfObject[0].getClass().getField("name");
                for (int j = 0; j < arrayOfObject.length; j++) {
                  this.al[j] = ((String)localField.get(arrayOfObject[j]));
                }
              }
            }
            return;
          }
          catch (Exception localException)
          {
            localException.printStackTrace();
            App.reportThrowable(localException);
            xinit_done();
          }
        }
        while (bindService(GoogleLoginServiceConstants.SERVICE_INTENT, new ServiceConnection()
        {
          public void onServiceConnected(ComponentName paramAnonymousComponentName, IBinder paramAnonymousIBinder)
          {
            IGoogleLoginService localIGoogleLoginService = IGoogleLoginService.Stub.asInterface(paramAnonymousIBinder);
            try
            {
              ActivityCloudPrinters.access$302(ActivityCloudPrinters.this, localIGoogleLoginService.getAccounts());
              ActivityCloudPrinters.this.xinit_done();
              ActivityCloudPrinters.this.unbindService(this);
              return;
            }
            catch (RemoteException localRemoteException)
            {
              for (;;)
              {
                localRemoteException.printStackTrace();
                App.reportThrowable(localRemoteException);
              }
            }
          }
          
          public void onServiceDisconnected(ComponentName paramAnonymousComponentName) {}
        }, 1)) {}
        xinit_done();
        return;
      }
      Vector localVector = new Vector();
      for (int i = 1;; i++)
      {
        String str = this.prefs.getString("gls_account_" + i, null);
        if (str == null) {
          break;
        }
        localVector.add(str);
      }
      this.al = ((String[])localVector.toArray(new String[0]));
      xinit_done();
      return;
    }
    catch (InterruptedException localInterruptedException)
    {
      break label6;
    }
  }
  
  private void xinit_done()
  {
    this.wt = null;
    runOnUiThread(new Runnable()
    {
      public void run()
      {
        String str1 = ActivityCloudPrinters.this.prefs.getString("cloud_account", null);
        String[] arrayOfString = ActivityCloudPrinters.this.al;
        String str2 = null;
        int j;
        if (arrayOfString != null)
        {
          int i = ActivityCloudPrinters.this.al.length;
          str2 = null;
          if (i > 0)
          {
            j = 0;
            int k = ActivityCloudPrinters.this.al.length;
            str2 = null;
            if (j < k)
            {
              if (!ActivityCloudPrinters.this.al[j].equals(str1)) {
                break label101;
              }
              str2 = str1;
            }
          }
        }
        if (str2 != null) {
          ActivityCloudPrinters.this.load_printers(str2);
        }
        label101:
        do
        {
          return;
          j++;
          break;
          ActivityCloudPrinters.this.hideProgress();
        } while (ActivityCloudPrinters.this.isFinishing());
        ActivityCloudPrinters.this.findViewById(2131230803).showContextMenu();
      }
    });
  }
  
  protected void init()
  {
    if (token == null) {
      super.init();
    }
  }
  
  public void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    this.handler = new Handler();
    setContentView(2130903065);
    setTitle(2131099674);
    ((Button)findViewById(2131230802)).setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramAnonymousView)
      {
        if (ActivityCloudPrinters.this.account != null) {
          ActivityCloudPrinters.this.load_printers(ActivityCloudPrinters.this.account);
        }
      }
    });
    Button localButton = (Button)findViewById(2131230803);
    localButton.setOnCreateContextMenuListener(this);
    localButton.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramAnonymousView)
      {
        paramAnonymousView.showContextMenu();
      }
    });
    ListView localListView = (ListView)findViewById(2131230805);
    localListView.setAdapter(new PrinterAdapter(this));
    localListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
      public void onItemClick(AdapterView<?> paramAnonymousAdapterView, View paramAnonymousView, int paramAnonymousInt, long paramAnonymousLong)
      {
        Printer localPrinter = (Printer)paramAnonymousView.getTag();
        final String str = localPrinter.id.substring(0, localPrinter.id.indexOf("@"));
        ActivityCloudPrinters.this.showProgress(ActivityCloudPrinters.this.getResources().getString(2131099746));
        ActivityCloudPrinters.access$202(ActivityCloudPrinters.this, new Thread()
        {
          public void run()
          {
            ActivityCloudPrinters.this.last_error = null;
            try
            {
              BasicHttpParams localBasicHttpParams = new BasicHttpParams();
              HttpConnectionParams.setConnectionTimeout(localBasicHttpParams, 15000);
              HttpConnectionParams.setSoTimeout(localBasicHttpParams, 15000);
              DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient(localBasicHttpParams);
              HttpGet localHttpGet = new HttpGet("https://www.google.com/cloudprint/printer?output=json&printerid=" + str);
              localHttpGet.setHeader("User-Agent", App.getUserAgent());
              localHttpGet.setHeader("Authorization", "GoogleLogin auth=" + ActivityCloudPrinters.this.prefs.getString("cloud_token", ""));
              localHttpGet.setHeader("X-CloudPrint-Proxy", "PrinterShare");
              HttpResponse localHttpResponse = localDefaultHttpClient.execute(localHttpGet);
              localStatusLine = localHttpResponse.getStatusLine();
              if (localStatusLine.getStatusCode() != 200) {
                break label273;
              }
              localGPrinterList = (ActivityCloudPrinters.GPrinterList)Json.read(new BufferedInputStream(localHttpResponse.getEntity().getContent()), ActivityCloudPrinters.GPrinterList.class);
              if (localGPrinterList.printers == null) {
                break label323;
              }
              i = 0;
            }
            catch (Exception localException)
            {
              for (;;)
              {
                StatusLine localStatusLine;
                ActivityCloudPrinters.GPrinterList localGPrinterList;
                int i;
                final Printer localPrinter;
                label273:
                label323:
                localException.printStackTrace();
                App.reportThrowable(localException);
                continue;
                i++;
              }
            }
            if (i < localGPrinterList.printers.size())
            {
              localPrinter = ActivityCloudPrinters.loadPrinter((ActivityCloudPrinters.GPrinter)localGPrinterList.printers.get(i));
              if (localPrinter.id.startsWith(str))
              {
                ActivityCloudPrinters.this.runOnUiThread(new Runnable()
                {
                  public void run()
                  {
                    ActivityCloudPrinters.this.hideProgress();
                    ActivityCloudPrinters.this.setPrinter(localPrinter);
                    ActivityCloudPrinters.this.setResult(-1);
                    ActivityCloudPrinters.this.finish();
                  }
                });
                return;
                ActivityCloudPrinters.this.last_error = ("Error: " + localStatusLine.getStatusCode() + " " + localStatusLine.getReasonPhrase());
              }
            }
            else
            {
              if (ActivityCloudPrinters.this.last_error == null) {
                ActivityCloudPrinters.this.last_error = "Error: Unknown error";
              }
              ActivityCloudPrinters.this.runOnUiThread(new Runnable()
              {
                public void run()
                {
                  ActivityCloudPrinters.this.hideProgress();
                  ActivityCloudPrinters.this.displayLastError(null);
                }
              });
              return;
            }
          }
        });
        ActivityCloudPrinters.this.wt.start();
      }
    });
    this.ftoken = false;
    this.printers = new Vector();
    showProgress(getResources().getString(2131099746));
  }
  
  public void onCreateContextMenu(ContextMenu paramContextMenu, View paramView, ContextMenu.ContextMenuInfo paramContextMenuInfo)
  {
    paramContextMenu.setHeaderTitle(getResources().getString(2131099664));
    if (this.al != null) {
      for (int i = 0; i < this.al.length; i++) {
        paramContextMenu.add(0, i + 1000, 0, this.al[i]);
      }
    }
    if ((!App.has_feature_gls) || (getSystemService("account") != null)) {
      paramContextMenu.add(0, 999, 0, 2131099665);
    }
  }
  
  public void onDestroy()
  {
    super.onDestroy();
  }
  
  public boolean onMenuItemSelected(int paramInt, MenuItem paramMenuItem)
  {
    int i = paramMenuItem.getItemId();
    if (i == 999)
    {
      if (App.has_feature_gls)
      {
        final Object localObject = getSystemService("account");
        if (localObject != null)
        {
          this.wt = new Thread()
          {
            public void run()
            {
              try
              {
                Method[] arrayOfMethod = localObject.getClass().getMethods();
                for (int i = 0;; i++)
                {
                  int j = arrayOfMethod.length;
                  Method localMethod = null;
                  if (i < j)
                  {
                    if ("addAccount".equals(arrayOfMethod[i].getName())) {
                      localMethod = arrayOfMethod[i];
                    }
                  }
                  else
                  {
                    Object localObject1 = localObject;
                    Object[] arrayOfObject = new Object[7];
                    arrayOfObject[0] = "com.google";
                    arrayOfObject[1] = null;
                    arrayOfObject[2] = null;
                    arrayOfObject[3] = null;
                    arrayOfObject[4] = ActivityCloudPrinters.this;
                    arrayOfObject[5] = null;
                    arrayOfObject[6] = null;
                    Object localObject2 = localMethod.invoke(localObject1, arrayOfObject);
                    String str = ((Bundle)localObject2.getClass().getMethod("getResult", new Class[0]).invoke(localObject2, new Object[0])).getString("authAccount");
                    if (str != null)
                    {
                      SharedPreferences.Editor localEditor = ActivityCloudPrinters.this.prefs.edit();
                      localEditor.putString("cloud_account", str);
                      localEditor.commit();
                      ActivityCloudPrinters.this.xinit();
                    }
                    return;
                  }
                }
                return;
              }
              catch (Exception localException)
              {
                localException.printStackTrace();
                App.reportThrowable(localException);
              }
            }
          };
          this.wt.start();
        }
        return true;
      }
      load_printers(null);
      return true;
    }
    if (i >= 1000)
    {
      load_printers(this.al[(i - 1000)]);
      return true;
    }
    return super.onMenuItemSelected(paramInt, paramMenuItem);
  }
  
  protected void onResume()
  {
    super.onResume();
    if ((this.al == null) || (requesting_credentials != 0))
    {
      showProgress(getResources().getString(2131099746));
      this.wt = new Thread()
      {
        public void run()
        {
          ActivityCloudPrinters.this.xinit();
        }
      };
      this.wt.start();
    }
  }
  
  public static class GCapability
  {
    public Hashtable<String, String> $other;
    public String name;
    public ArrayList<ActivityCloudPrinters.GOption> options;
    public String type;
    public String value;
  }
  
  public static class GOption
  {
    public Hashtable<String, String> $other;
    public String default_;
    public String name;
    public ActivityCloudPrinters.GProperties scoredProperties;
  }
  
  public static class GPrinter
  {
    public ArrayList<ActivityCloudPrinters.GCapability> capabilities;
    public String capsFormat;
    public String description;
    public String displayName;
    public String id;
    public String name;
    public String proxy;
    public String status;
    public String type;
  }
  
  public static class GPrinterList
  {
    public ArrayList<ActivityCloudPrinters.GPrinter> printers;
    public String success;
  }
  
  public static class GProperties
  {
    public Hashtable<String, String> $other;
  }
  
  class PrinterAdapter
    implements ListAdapter
  {
    public Context mContext;
    private List<DataSetObserver> observers = null;
    
    public PrinterAdapter(Context paramContext)
    {
      this.mContext = paramContext;
    }
    
    public boolean areAllItemsEnabled()
    {
      return true;
    }
    
    public void fireOnChanged()
    {
      if (this.observers != null)
      {
        int i = this.observers.size();
        for (int j = 0; j < i; j++) {
          ((DataSetObserver)this.observers.get(j)).onChanged();
        }
      }
    }
    
    public int getCount()
    {
      if (ActivityCloudPrinters.this.printers != null) {
        return ActivityCloudPrinters.this.printers.size();
      }
      return 0;
    }
    
    public Object getItem(int paramInt)
    {
      return ActivityCloudPrinters.this.printers.elementAt(paramInt);
    }
    
    public long getItemId(int paramInt)
    {
      return paramInt;
    }
    
    public int getItemViewType(int paramInt)
    {
      return 1;
    }
    
    public View getView(int paramInt, View paramView, ViewGroup paramViewGroup)
    {
      View localView1 = LayoutInflater.from(this.mContext).inflate(2130903064, null);
      Printer localPrinter = (Printer)ActivityCloudPrinters.this.printers.elementAt(paramInt);
      TextView localTextView1 = (TextView)localView1.findViewById(2131230800);
      String str1 = "[" + localPrinter.owner.login + "]";
      if ((localPrinter.owner.nick != null) && (localPrinter.owner.nick.length() > 0)) {
        str1 = localPrinter.owner.nick;
      }
      if ((localPrinter.owner.name != null) && (localPrinter.owner.name.length() > 0)) {
        str1 = localPrinter.owner.name;
      }
      localTextView1.setText(str1);
      ((TextView)localView1.findViewById(2131230758)).setText(localPrinter.title);
      TextView localTextView2 = (TextView)localView1.findViewById(2131230759);
      String str2 = "";
      if ((localPrinter.owner.country != null) && (localPrinter.owner.country.length() > 0)) {
        str2 = localPrinter.owner.country;
      }
      String str4;
      label296:
      int i;
      label349:
      int j;
      label380:
      View localView2;
      int k;
      if ((localPrinter.owner.city != null) && (localPrinter.owner.city.length() > 0))
      {
        StringBuilder localStringBuilder2 = new StringBuilder().append(localPrinter.owner.city);
        if (str2.length() > 0)
        {
          str4 = ", ";
          str2 = str4 + str2;
          if ((localPrinter.location != null) && (localPrinter.location.length() > 0)) {
            str2 = localPrinter.location;
          }
          if (str2 == null) {
            str2 = "";
          }
          localTextView2.setText(str2);
          if (str2.length() <= 0) {
            break label543;
          }
          i = 0;
          localTextView2.setVisibility(i);
          ImageView localImageView = (ImageView)localView1.findViewById(2131230760);
          if (!localPrinter.online) {
            break label550;
          }
          j = 2130837561;
          localImageView.setBackgroundResource(j);
          localView2 = localView1.findViewById(2131230801);
          if ((ActivityCore.printer == null) || (ActivityCore.printer.id == null)) {
            break label557;
          }
          boolean bool = ActivityCore.printer.id.equals(localPrinter.id);
          k = 0;
          if (!bool) {
            break label557;
          }
        }
      }
      for (;;)
      {
        localView2.setVisibility(k);
        localView1.setTag(localPrinter);
        return localView1;
        str4 = "";
        break;
        if ((localPrinter.owner.state == null) || (localPrinter.owner.state.length() <= 0)) {
          break label296;
        }
        StringBuilder localStringBuilder1 = new StringBuilder().append(localPrinter.owner.state);
        if (str2.length() > 0) {}
        for (String str3 = ", ";; str3 = "")
        {
          str2 = str3 + str2;
          break;
        }
        label543:
        i = 8;
        break label349;
        label550:
        j = 2130837560;
        break label380;
        label557:
        k = 4;
      }
    }
    
    public int getViewTypeCount()
    {
      return 1;
    }
    
    public boolean hasStableIds()
    {
      return false;
    }
    
    public boolean isEmpty()
    {
      return (ActivityCloudPrinters.this.printers == null) || (ActivityCloudPrinters.this.printers.size() == 0);
    }
    
    public boolean isEnabled(int paramInt)
    {
      return true;
    }
    
    public void registerDataSetObserver(DataSetObserver paramDataSetObserver)
    {
      if (this.observers == null) {
        this.observers = new ArrayList();
      }
      if (!this.observers.contains(paramDataSetObserver)) {
        this.observers.add(paramDataSetObserver);
      }
    }
    
    public void unregisterDataSetObserver(DataSetObserver paramDataSetObserver)
    {
      if (this.observers == null) {
        return;
      }
      this.observers.remove(paramDataSetObserver);
    }
  }
}


/* Location:           C:\Users\Admin\Desktop\Decompiler\JAD\jd-gui-0.3.6.windows\classes_dex2jar.jar
 * Qualified Name:     com.dynamixsoftware.printershare.ActivityCloudPrinters
 * JD-Core Version:    0.7.0.1
 */